package com.easybbs.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.easybbs.entity.dto.PyReadDTO;
import com.easybbs.entity.po.NewsPO;
import com.easybbs.mappers.NewsMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

@Service
@Slf4j
public class NewsService {

    @Resource
    NewsMapper newsMapper;

    public void addNews(NewsPO newsPO){
        newsMapper.insertNews(newsPO);
    }

    public   Map<String,List<NewsPO>> get() {
        Map<String,List<NewsPO>> listMap=new HashMap<>();
        List<NewsPO> mainNews = newsMapper.selectMainNews();
        List<NewsPO> center1 = newsMapper.selectCenter1();
        List<NewsPO> center2 = newsMapper.selectCenter2();
        List<NewsPO> right = newsMapper.selectRight();
        List<NewsPO> bottom1 = newsMapper.selectBottom1();
        List<NewsPO> bottom2 = newsMapper.selectBottom2();
        List<NewsPO> bottom3 = newsMapper.selectBottom3();
        List<NewsPO> bottom4 = newsMapper.selectBottom4();
        listMap.put("main",mainNews);
        listMap.put("center1",center1);
        listMap.put("center2",center2);
        listMap.put("right",right);
        listMap.put("bottom1",bottom1);
        listMap.put("bottom2",bottom2);
        listMap.put("bottom3",bottom3);
        listMap.put("bottom4",bottom4);
        return listMap;
    }

    public NewsPO getNewsDetail(int newsId) {
       return newsMapper.selectSingleNews(newsId);
    }

    public String pyRead(PyReadDTO pyReadDTO) throws IOException {
        boolean validPos = validPos(pyReadDTO.getPos());
        if (!validPos){
            return "pos输入错误";
        }
        String pyUrl="http://localhost:5000/scrape?url="+pyReadDTO.getUrl();
        // 创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 创建请求对象
        HttpGet httpGet = new HttpGet(pyUrl);
        // 设置请求头，指定返回格式为UTF-8
        httpGet.addHeader("Accept-Charset", "UTF-8");
        CloseableHttpResponse response = httpClient.execute(httpGet);
        // 获取状态码
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != 200) {
            log.info("请求异常");
        }
        // 将响应实体转换为字符串
        String json = EntityUtils.toString(response.getEntity(), "UTF-8");

        List<NewsPO> newsPOS= JSONObject.parseArray(json,NewsPO.class);
        String pos = pyReadDTO.getPos();
        String tag = pyReadDTO.getTag();
        newsPOS.forEach(newsPO -> {
            newsPO.setImg("http://tcm.china.com.cn/pic/2024-02/26/abfe32b3-4718-421a-9edd-5c565929a6d0.jpeg");
            newsPO.setPos(pos);
            newsPO.setTag(tag);
            newsPO.setCtime(new Date());
            newsMapper.insertNews(newsPO);
        });
        return "success";
    }

    public boolean validPos(String tag){
        String[] tags=new String[]{ "main","center1","center2","right","bottom1","bottom2","bottom3","bottom4"};
        List<String> stringList = Arrays.asList(tags);
        return stringList.contains(tag);
    }
}
